<?php
namespace app\controller;

use app\BaseController;
use app\model\UserLogs;
use app\model\Users;
use think\facade\View;
use think\Request;

class Cost extends BaseController
{
    static $users;          //用户表
    static $user_logs;      //用户log表

    public function __construct(array $data = [])
    {
        self::$users = new Users();
        self::$user_logs = new UserLogs();
    }

    //金额充值
    public function cost(Request $request)
    {
        if (is_get()){
            //视图
            $mobile = intval($request->param('m'));
            $error = '用户不存在！';
            if (strlen($mobile) != 11){
                $error = '手机号格式不正确';
            }
            $user_info = self::$users->get_info(['mobile'=>$mobile],'id,name,mobile,total_cost,create_time');
            if (!$user_info){
                $user_info =  [];
            }else{
                $user_info = $user_info->toArray();
                $error = '';
            }
            View::assign('error',$error);
            View::assign('mobile',$mobile);
            View::assign('user_info',$user_info);
            return View::fetch();
        }else{
            //执行接口操作
            $action = getv('action');
            if ($action == 'name'){
                //获取用户名
                $mobile = getv('mobile');
                $cost = getv('cost');
                $user_info = self::$users->get_info(['mobile'=>$mobile],'id,name,mobile,total_cost');
                $user_info['total_cost']?:$user_info['total_cost']=0;
                $result_cost = $cost + $user_info['total_cost'];
                if (!$user_info){
                    returnJson(false,2,'用户不存在~');
                }else{
                    $user_info = $user_info->toArray();
                    returnJson(true,0,'ok~',['id'=>$user_info['id'],'name'=>$user_info['name'],'total_cost'=>$user_info['total_cost'],'result_cost'=>$result_cost]);
                }
            }else{
                //金额变更操作
                self::$users->startTrans();
                self::$user_logs->startTrans();
                try {
                    $mobile = getv('mobile');
                    $cost = getv('cost');
                    param_empty([$mobile,$cost]);
                    $user_info = self::$users->get_info(['mobile'=>$mobile],'id,name,total_cost');
                    if (!$user_info){
                        returnJson(false,2,'用户不存在~');
                    }
                    $data['id'] = $user_info['id'];
                    $data['total_cost'] = $user_info['total_cost'] + $cost;
                    self::$users->update($data);
                    $log['uid'] = $user_info['id'];
                    $log['type'] = 1;
                    $log['time'] = time();
                    $log['des'] = '金额充值';
                    $log['cost'] = $cost;
                    self::$user_logs->save($log);
                    //提交事务
                    self::$users->commit();
                    self::$user_logs->commit();
                    returnJson(true,0,'充值成功~');
                }catch (\Exception $e){
                    //事务回滚
                    self::$users->rollback();
                    self::$user_logs->rollback();
                }

            }
        }
    }


    //用户消费
    public function payout(Request $request)
    {
        //金额变更操作
        self::$users->startTrans();
        self::$user_logs->startTrans();
        try {
            $id = getv('id');
            $cost = getv('cost');
            param_empty([$id,$cost]);
            $user_info = self::$users->get_info(['id'=>$id],'id,total_cost');
            if (!$user_info){
                returnJson(false,2,'用户不存在~');
            }
            if ($user_info['total_cost'] < $cost){
                returnJson(false,3,'用户余额不足！');
            }
            $data['id'] = $user_info['id'];
            $data['total_cost'] = $user_info['total_cost'] - $cost;
            self::$users->update($data);
            $log['uid'] = $user_info['id'];
            $log['type'] = 1;
            $log['time'] = time();
            $log['des'] = '金额消费';
            $log['cost'] = '-'.$cost;
            self::$user_logs->save($log);
            //提交事务
            self::$users->commit();
            self::$user_logs->commit();
            returnJson(true,0,'消费成功~');
        }catch (\Exception $e){
            //事务回滚
            self::$users->rollback();
            self::$user_logs->rollback();
        }
    }
}
